import dayjs from "dayjs";
import request from "../../utils/http";
import { anyObj, enter, wxToast } from "../../utils/util";

// pages/chooseScheduleDate/chooseScheduleDate.ts
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		minDate: new Date().getTime(),
		date: '',
		dateTimestamp: 0,
		query: <anyObj>{},
		timeArr: <string[]>[],
		actTime: ''
	},

	async handleSelect(e: WechatMiniprogram.CustomEvent) {
		const { value } = e.detail;
		const date = dayjs(value).format('YYYY-MM-DD');
		console.log('date: ', date);
		try {
			const timeArr = await request({
				url: '/api/customers/reservation/getReservationTimes',
				method: 'POST',
				data: {
					outletId: this.data.query.outletId,
					orderId: this.data.query.orderId,
					serviceId: this.data.query.serviceId,
					date
				},
				contentTypeForm: true,
				errToast: true,
				showLoading: {title: '正在获取服务时间...'}
			});
			if (!Array.isArray(timeArr) || timeArr.length < 1) {
				wxToast({icon: 'error', title: '暂无可预约的时间'});
			}
			this.setData({
				date,
				dateTimestamp: value,
				timeArr,
				actTime: ''
			});
		} catch (error) {
			this.setData({
				date,
				timeArr: [],
				actTime: ''
			});
		}
	},

	onChangeTime(e: WechatMiniprogram.CustomEvent) {
		this.setData({
			actTime: e.detail.value
		});
	},

	async submit() {
		const data = this.data;
		if (!data.actTime) {
			return wxToast({icon: 'error', title: '请选择服务时间'});
		}
		try {
			await request({
				url: '/api/customers/reservation/saveReservationTime',
				method: 'POST',
				data: {
					...data.query,
					time: data.actTime,
					date: data.date
				},
				contentTypeForm: false,
				errToast: true,
				showLoading: {title: '正在预约...'}
			});
			enter({
				url: '/pages/bookSuccessfully/bookSuccessfully',
				query: data.query
			});
		} catch (error) {
			
		}
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad(query = {}) {
		this.setData({
			query
		});
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow() {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage() {

	}
})